package com.sun.sc.controller;

import com.sun.sc.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @GetMapping("/register/{username}/{password}/{code}")
    public Integer register(HttpServletResponse response, @PathVariable String username, @PathVariable String password, @PathVariable String code) {
        return userService.register(response, username, password, code);
    }

    @GetMapping("/login/{email}/{password}")
    public String login(HttpServletResponse response, @PathVariable String email, @PathVariable String password) {
        System.out.println("username = " + email + "password = " + password);
        String value = userService.login(response, email, password);
        try {
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return value;
    }

    /**
     * 根据token查询⽤户登录邮箱地址接⼝
     *
     * @return 邮箱地址
     */
    @GetMapping("/info/{token}")
    public String getEmailByToken(@PathVariable String token) {
        return userService.getEmailByToken(token);
    }

}
